﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Enterprise.Model.WorkFlow;
using Enterprise.Data.WorkFlow;
using Enterprise.Infrastructure;
using System.Data;
namespace Enterprise.Service.WorkFlow
{
    /// <summary>
    /// 用户流程授权服务
    /// </summary>
    public class UserWfConfigService
    {

        /// <summary>
        /// 流程节点缓存标记
        /// </summary>
        private static string cacheKey = "Enterprise-UserWfConfig";

        /// <summary>
        /// 流程列表 返回所有流程记录
        /// </summary>
        /// <returns></returns>
        public List<UserWfConfigEntity> UserWfConfigList()
        {
            if (CacheHelper.Contains(cacheKey))
            {
                return (List<UserWfConfigEntity>)CacheHelper.GetCache(cacheKey);
            }
            else
            {
                UserWfConfigData data = new UserWfConfigData();
                List<UserWfConfigEntity> list = data.UserWfConfigList();
                CacheHelper.Add(cacheKey, list);
                return list;
            }
        }

        /// <summary>
        /// 返回流程记录信息
        /// </summary>
        /// <param name="Id">流程编码</param>
        /// <returns></returns>
        public List<UserWfConfigEntity> UserWfConfig(int UserId)
        {
            return UserWfConfigList().Where(p => p.USERID == UserId).ToList();
        }

        /// <summary>
        /// 清楚缓存
        /// </summary>
        private void ClearCache()
        {
            CacheHelper.RemoveCache(cacheKey);
        }

        /// <summary>
        /// 流程操作 添加、修改、删除
        /// </summary>
        /// <param name="entity">用户实体</param>
        public void UserWfConfigExecute(UserWfConfigEntity entity)
        {
            string cmd = string.Empty;
            switch (entity.DB_Option_Action)
            {
                case "Insert":
                    cmd = "Insert into SYS_USERWFCONFIG(CID,USERID,WFTYPEID,SQUSERID,STARTIME,ENDTIME,ISUSED) values(SEQ_SYS_USERWFCONFIG.nextval," + entity.USERID + "," + entity.WFTYPEID + "," + entity.SQUSERID + "," + Utility.FormatOracleDate(entity.STARTIME) + "," + Utility.FormatOracleDate(entity.ENDTIME) + "," + entity.ISUSED + ")";
                    break;
                case "DeleteUser":
                    cmd = "Delete from SYS_USERWFCONFIG where USERID=" + entity.USERID;
                    break;
                case "DeleteId":
                    cmd = "Delete from SYS_USERWFCONFIG where CID=" + entity.CID;
                    break;
            }
            if (!string.IsNullOrEmpty(cmd))
            {
                UserWfConfigExecuteBySql(cmd, 1);
            }
        }

        /// <summary>
        /// 操作方法
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="typeId">1:刷新缓存 0:不刷新缓存 </param>
        public void UserWfConfigExecuteBySql(string cmd, int typeId)
        {
            WorkFlowNodeData data = new WorkFlowNodeData();
            data.WorkFlowExecute(cmd);
            if (typeId == 1)
            {
                ClearCache();
            }
        }

    }
}
